<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="118px">
      <el-form-item label="姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学号" prop="studentNumber">
        <el-input
          v-model="queryParams.studentNumber"
          placeholder="请输入学号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专业" prop="studentMajor">
        <el-select v-model="queryParams.studentMajor" style="width: 190px" placeholder="请选择专业" clearable >
          <el-option v-for="dict in sys_major" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="studentClass">
        <el-select v-model="queryParams.studentClass" placeholder="请选择班级" clearable style="width: 190px">
          <el-option v-for="dict in sys_class" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="辅导员" prop="instructorId">
        <el-input
          v-model="queryParams.instructorId"
          placeholder="请输入辅导员"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="就业导师" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入就业导师"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="导师部门" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入导师部门"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="困难毕业生" prop="difficultFlag">
        <el-select v-model="queryParams.difficultFlag"  clearable style="width:192px;">
          <el-option
            v-for="dict in sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="省级优秀毕业生" prop="provinceHonorFlag" >
        <el-select v-model="queryParams.provinceHonorFlag"  clearable style="width:192px;">
          <el-option
            v-for="dict in sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="中共党员" prop="cpcFlag">
        <el-select v-model="queryParams.cpcFlag"  clearable style="width:192px;">
          <el-option
            v-for="dict in sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="就业方式" prop="employMode">
        <el-select v-model="queryParams.employMode" placeholder="请选择就业方式" clearable style="width:192px;">
          <el-option
            v-for="dict in employment_way"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="配合度" prop="adaptDegree">
        <el-select v-model="queryParams.adaptDegree" placeholder="请选择配合度" clearable style="width:192px;">
          <el-option
            v-for="dict in adapt_degree"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="目前状态" prop="unEmployReason">
        <el-select v-model="queryParams.unEmployReason" placeholder="请选择目前状态" clearable style="width:192px;">
          <el-option
            v-for="dict in unemployee_reason"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="就业意向" prop="employWill">
        <el-select v-model="queryParams.employWill" placeholder="请选择就业意向" clearable style="width:192px;">
          <el-option
            v-for="dict in employ_will"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['employment:employment:add']"
        >新增</el-button>
      </el-col>
      -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['employment:employment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['employment:employment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['employment:employment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="employmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号" align="center" prop="id" /> -->
      <el-table-column label="姓名" align="center" prop="studentName" />
      <el-table-column label="学号" align="center" prop="studentNumber" />
      <el-table-column label="专业" align="center" prop="studentMajor" >
        <template #default="scope">
          <dict-tag :options="sys_major" :value="scope.row.studentMajor"/>
        </template>
      </el-table-column>
      <el-table-column label="班级" align="center" prop="studentClass" >
        <template #default="scope">
          <dict-tag :options="sys_class" :value="scope.row.studentClass"/>
        </template>
        </el-table-column>
      <el-table-column label="本专科" align="center" prop="studentLevel">
        <template #default="scope">
          <dict-tag :options="sys_undergraduate_specialist" :value="scope.row.studentLevel"/>
        </template>
      </el-table-column>
       <el-table-column label="困难毕业生" align="center" prop="difficultFlag">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.difficultFlag" />
        </template>
      </el-table-column>
      <el-table-column label="省级优秀毕业生" align="center" prop="provinceHonorFlag">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.provinceHonorFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="中共党员" align="center" prop="cpcFlag">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.cpcFlag"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="简历" align="center" prop="resume" /> -->
      <el-table-column label="学生联系方式" align="center" prop="phonenumber" />
      <el-table-column label="家长联系方式" align="center" prop="parentPhone" />
      <el-table-column label="就业方式" align="center" prop="employMode">
        <template #default="scope">
          <dict-tag :options="employment_way" :value="scope.row.employMode"/>
        </template>
      </el-table-column>
      <el-table-column label="签约单位" align="center" prop="signCompany" />
      <el-table-column label="未就业原因" align="center" prop="unEmployReason">
        <template #default="scope">
          <dict-tag :options="unemployee_reason" :value="scope.row.unEmployReason"/>
        </template>
      </el-table-column>
      <el-table-column label="就业意向" align="center" prop="employWill">
        <template #default="scope">
          <dict-tag :options="employ_will" :value="scope.row.employWill"/>
        </template>
      </el-table-column>
      <el-table-column label="简历投递次数" align="center" prop="resumeSendCount" />
      <el-table-column label="就业导师" align="center" prop="employTeacherName" />
      <el-table-column label="导师部门" align="center" prop="employTeacherDept" />
      <el-table-column label="辅导员" align="center" prop="counselor" />
      <el-table-column label="配合度" align="center" prop="adaptDegree">
        <template #default="scope">
          <dict-tag :options="adapt_degree" :value="scope.row.adaptDegree"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['employment:employment:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['employment:employment:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生就业信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="employmentRef" :model="form" :rules="rules" label-width="110px">
        <!-- <el-form-item label="学生id" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学生id" />
        </el-form-item> -->

        <el-form-item label="简历" prop="resume">
          <file-upload v-model="form.resume"/>
        </el-form-item>
        <el-form-item label="就业方式" prop="employMode">
          <el-select v-model="form.employMode" placeholder="请选择就业方式">
            <el-option
              v-for="dict in employment_way"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="签约单位" prop="signCompany">
          <el-input v-model="form.signCompany" placeholder="请输入签约单位" />
        </el-form-item>
        <el-form-item label="未就业原因" prop="unEmployReason">
          <el-select v-model="form.unEmployReason" placeholder="请选择未就业原因">
            <el-option
              v-for="dict in unemployee_reason"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="就业意向" prop="employWill">
          <el-select v-model="form.employWill" placeholder="请选择就业意向">
            <el-option
              v-for="dict in employ_will"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="简历投递次数" prop="resumeSendCount">
          <el-input v-model="form.resumeSendCount" placeholder="请输入简历投递次数" />
        </el-form-item>
        <el-form-item label="导师部门" prop="employTeacherDeptId">
          <el-tree-select v-model="form.employTeacherDept" :data="deptOptions" filterable
            :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id" placeholder="请选择导师部门"
            check-strictly @change="handleAdvisorDepartmentChange" />
          <!-- <el-input v-model="form.advisorDepartmentId" placeholder="请输入导师部门" /> -->
        </el-form-item>
        <el-form-item label="就业导师" prop="employTeacher">
          <el-select v-model="form.employTeacherName" placeholder="请选择就业导师" clearable @focus="handleFocus"
          @change="handleEmployTeacherChange">
            <el-option v-for="academicAdvisor in academicAdvisorList" :key="academicAdvisor.userId"
              :label="academicAdvisor.nickName" :value="academicAdvisor.userId" />
          </el-select>
          <!-- <el-input v-model="form.employTeacher" placeholder="请输入就业导师" /> -->
        </el-form-item>
        <el-form-item label="配合度" prop="adaptDegree">
          <el-select v-model="form.adaptDegree" placeholder="请选择配合度">
            <el-option
              v-for="dict in adapt_degree"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Employment">
import { listEmployment, getEmployment, delEmployment, addEmployment, updateEmployment } from "@/api/employment/employment";
import { deptTreeSelect } from "@/api/system/user";
import { ElMessage } from 'element-plus'
import { getAdvisorList } from "@/api/student/student";

const { proxy } = getCurrentInstance();
const { sys_yes_no, employment_way, adapt_degree, sys_major, sys_class,sys_undergraduate_specialist,unemployee_reason,employ_will } = proxy.useDict('sys_yes_no', 'employment_way', 'adapt_degree','sys_major','sys_class','sys_undergraduate_specialist','unemployee_reason','employ_will');

const employmentList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const deptOptions = ref(undefined);
const academicAdvisorList = ref(undefined);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    difficultFlag: null,
    provinceHonorFlag: null,
    cpcFlag: null,
    employMode: null,
    signCompany: null,
    unEmployReason: null,
    resumeSendCount: null,
    adaptDegree: null,
  },
  rules: {
    resume: [
      { required: true, message: "请上传简历", trigger: "blur" }
    ],
    employMode:[
      { required: true, message: "请选择就业方式", trigger: "change" }
    ],
    unEmployReason: [
      { required: true, message: "请选择未就业原因", trigger: "change" }
    ],
    resumeSendCount: [
      { required: true, message: "简历投递次数不可为空", trigger: "blur" }
    ],
    employTeacher: [
      { required: true, message: "请选择就业导师", trigger: "change" }
    ],
    adaptDegree: [
      { required: true, message: "请选择配合度", trigger: "change" }
    ],

  }
});

const { queryParams, form, rules } = toRefs(data);

const handleAdvisorDepartmentChange = (val) => {
  form.value.employTeacherDeptId = val;
  form.value.employTeacher = null;
  form.value.employTeacherName = null;
}

const handleEmployTeacherChange = (val) => {
  form.value.employTeacher = val;
}
/** 查询学生就业信息列表 */
function getList() {
  loading.value = true;
  listEmployment(queryParams.value).then(response => {
    employmentList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    studentId: null,
    difficultFlag: null,
    provinceHonorFlag: null,
    cpcFlag: null,
    resume: null,
    employMode: null,
    signCompany: null,
    unEmployReason: null,
    resumeSendCount: null,
    employTeacher: null,
    adaptDegree: null,
    remark: null
  };
  proxy.resetForm("employmentRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加学生就业信息";
  getDeptTree();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getEmployment(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改学生就业信息";
    getDeptTree();
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["employmentRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateEmployment(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addEmployment(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除学生就业信息编号为"' + _ids + '"的数据项？').then(function() {
    return delEmployment(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('employment/employment/export', {
    ...queryParams.value
  }, `employment_${new Date().getTime()}.xlsx`)
}

/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
  });
}

//获取学业导师列表
function loadAdvisorList(deptId) {
  getAdvisorList(deptId).then(response => {
    academicAdvisorList.value = response;
  });
}

/** 查询用户列表 */
const handleFocus = () => {
  if (form.value.employTeacherDeptId) {
    loadAdvisorList(form.value.employTeacherDeptId)
  } else {
    ElMessage('请先选择部门');
  }
}
getList();
</script>
